import os, sys
import pygame
from pygame.locals import *
from time import sleep
from jasgame.io import ImageLoader
from jasgame.core import BaseJasGame

class AlphaJasGame(BaseJasGame):
    def __init__(self):
        BaseJasGame.__init__(self, 'AlphaJas')
        self.write_caption("Hi Jazzy, type a key ..")
        self.clock = pygame.time.Clock()
        which= K_j
        self.newSprite(which)
    
    def newSprite(self, which):
        self.img, self.captiontext = self.whichImage(which)
        self.sprite = AlphaSprite(self.img)
        self.allsprites = pygame.sprite.RenderPlain((self.sprite))
        self.write_caption(self.captiontext)
        self.redraw()
        
    #returns False to stop, True to continue
    def handle_events(self):
        for event in pygame.event.get():
            if event.type == QUIT:
                return False
            elif event.type == KEYDOWN and event.key == K_ESCAPE:
                return True
            elif event.type == KEYUP and event.key == K_ESCAPE:
                return False
            elif event.type == KEYDOWN:
                img,text= self.whichImage(event.key)
                if img == self.img:
                    self.sprite.punched()
                else:
                    self.newSprite(event.key)
            elif event.type == KEYUP:
                return True
        self.redraw()
        return True
        
    def whichImage(self, which):
        #print which
        if which == K_d:
            return 'd.png', 'D for Daddy'
        elif which == K_b:
            return 'b.png', 'B for Baba'
        elif which == K_a:
            return 'a.png', 'A for Anna'
        elif which == K_m:
            return 'm.png', 'M for Mummy'
        elif which == K_j:
            return 'j.png', 'J for Jazzy'
        else:
            return 'Mallyon-kit.png', 'Oh, hi biaow'
    
class AlphaSprite(pygame.sprite.Sprite):
    def __init__(self, which):
        pygame.sprite.Sprite.__init__(self) #call Sprite initializer
        il= ImageLoader()
        self.image, self.rect = il.load_png(which)
        self.punching = 0
        screen = pygame.display.get_surface()
        self.area = screen.get_rect()
        self.rect.topleft = 10, 10
        self.move = 9
        self.movev = 18
        self.dizzy = 0
        self.bouncy = 0
        
    def update(self):
        pos = pygame.mouse.get_pos()
        self.rect.midtop = pos
        if self.punching:
            self.rect.move_ip(5, 10)

    def punch(self):
        if not self.punching:
            self.punching = 1
            hitbox = self.rect.inflate(-50, -50)
            self.punched()
            #return hitbox.colliderect(target.rect)

    def unpunch(self):
        self.punching = 0

    def unbounce(self):
        self.bouncy = 0
        #self.update()

    def update(self):
        "walk or spin, depending on the state"
        if self.dizzy:
            self._spin()
        elif self.bouncy:
            self._bounce()
        else:
            self._walk()
            
    def _walk(self):
        newpos = self.rect.move((self.move, 0))
        if self.rect.left < self.area.left or \
        self.rect.right > self.area.right:
            self.move = -self.move
            newpos = self.rect.move((self.move, 0))
            self.image = pygame.transform.flip(self.image, 1, 0)
        self.rect = newpos
    
    def _bounce(self):
        newpos = self.rect.move((0,self.movev))
        if self.rect.top < self.area.top or \
        self.rect.bottom > self.area.bottom:
            self.movev = -self.movev
            newpos = self.rect.move((0,self.movev))
            self.image = pygame.transform.flip(self.image, 1, 0)
        self.rect = newpos
        
        
    def _spin(self):
        "spin the biaow image"
        center = self.rect.center
        self.dizzy = self.dizzy + 12
        if self.dizzy >= 360:
            self.dizzy = 0
            self.image = self.original
            #alternate between bouncy and walky
            self.bouncy= 1 - self.bouncy 
        else:
            rotate = pygame.transform.rotate
            self.image = rotate(self.original, self.dizzy)
            self.image = rotate(self.original, self.dizzy)
        self.rect = self.image.get_rect(center=center)

    def punched(self):
        if not self.dizzy:
            self.dizzy = 1
            self.original = self.image

def main():
    bgame = AlphaJasGame()
    conti= True
    while conti:
        bgame.clock.tick(32)
        conti = bgame.handle_events()
    
    bgame.bye("bye bye jazzy")
    sys.exit()

#this calls the 'main' function when this script is executed
if __name__ == '__main__': main()
